package top.meeting.room.module.system.model;

import com.alibaba.fastjson.JSON;
import com.jfinal.kit.Kv;
import com.jfinal.plugin.activerecord.Record;
import com.jfinal.plugin.activerecord.SqlPara;
import com.jfinal.plugin.ehcache.CacheKit;
import top.meeting.room.baseModel.BaseForce;
import top.meeting.room.common.kit.ListKit;
import top.meeting.room.module.system.form.RoleForm;
import top.meeting.room.module.system.services.impl.ForceService;

import java.util.LinkedList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 我有故事，你有酒么？
 * JKhaled created by yunqisong@foxmail.com 2017/7/30
 * FOR : ForceBean
 */
public class Force extends BaseForce<Force> {
    public static final Force dao = new Force().dao();

    public static final Integer force_root = 1;

    public static final Integer force_menu = 2;

    public static final Integer force_button = 3;
    /**
     * SqlParas And Caches
     */
    public static final String force_tree = "force.tree";
    public static final String force_group = "force.group";
    public static final String force_getModel = "force.getModel";
    public static final String force_sonForces = "force.sonForces";
    public static final String force_pageModel = "force.pageModel";
    public static final String force_getRecord = "force.getRecord";
    public static final String force_pageRecord = "force.pageRecord";
    public static final String force_deleteByIds = "force.deleteByIds";
    public static final String force_findByForceName = "force.findByForceName";

    /**
     * 递归求子权限
     *
     * @return
     */
    public List<Record> getSonForces() {
        List<Record> retVal = null;
        if (this.getForceType() == force_button) return retVal;
        retVal = CacheKit.get(force_sonForces, String.valueOf(this.getSons()));
        List<Long> ids = ListKit.ids2List(this.getSons());
        if ((retVal == null || retVal.size() == 0) && ids != null && ids.size() > 0) {
            SqlPara sqlPara = Force.dao.getSqlPara(force_sonForces, Kv.by("ids", ids));
            List<Force> sonForces = Force.dao.find(sqlPara.getSql(), sqlPara.getPara());
            retVal = sonForces.stream().map(force -> {
                return force.toBack();
            }).collect(Collectors.toList());
            CacheKit.put(force_sonForces, String.valueOf(this.getSons()), retVal);
        }
        return retVal;
    }

    /**
     * 后台DTO
     *
     * @return
     */
    public Record toBack() {
        return super.toRecord().set("sonForces", this.getSonForces());
    }

    /**
     * 通过 jsonForces String分组
     *
     * @param jsonForces
     * @return
     */
    public List<Record> group(String jsonForces) {
        List<Record> retVal = CacheKit.get(force_group, String.valueOf(jsonForces));
        if (retVal == null || retVal.size() == 0) {
            List<RoleForm.ForceRoots> forces = JSON.parseArray(String.valueOf(jsonForces), RoleForm.ForceRoots.class);
            if (forces != null && !forces.isEmpty()) {
                retVal = new LinkedList<>();
                for (RoleForm.ForceRoots forceRoots : forces) {
                    Force forceRoot = ForceService.me.getModel(forceRoots.getForce_root());
                    Record recordRoot = forceRoot == null ? null : forceRoot.toRecord().remove("sons");
                    if (recordRoot != null && forceRoots.getForce_menus() != null && forceRoots.getForce_menus().size() > 0) {
                        List<Record> sonForces = new LinkedList<Record>();
                        for (RoleForm.ForceMenu forceMenus : forceRoots.getForce_menus()) {
                            Force forceMenu = ForceService.me.getModel(forceMenus.getForce_menu());
                            Record recordMenu = forceMenu == null ? null : forceMenu.toRecord().remove("sons");
                            if (forceMenus.getForce_buttons() != null && !forceMenus.getForce_buttons().isEmpty()) {
                                SqlPara sqlPara = Force.dao.getSqlPara(force_group, Kv.by("ids", forceMenus.getForce_buttons()));
                                List<Force> forceButtons = Force.dao.find(sqlPara);
                                if (forceButtons != null || !forceButtons.isEmpty()) {
                                    recordMenu.set("sonForces", forceButtons);
                                }
                            }
                            if (recordMenu != null)
                                sonForces.add(recordMenu);
                        }
                        recordRoot.set("sonForces", sonForces);
                    }
                    if (recordRoot != null)
                        retVal.add(recordRoot);
                }
                CacheKit.put(force_group, String.valueOf(jsonForces), retVal);
            }
        }
        return retVal;
    }

}
